{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c5f0cc92",
   "metadata": {},
   "source": [
    "------\n",
    "* 2023-11-08\n",
    "* 课程：python语言\n",
    "* week08（10周）\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69bb440f",
   "metadata": {},
   "source": [
    "# 1. 本周内容\n",
    "## 1. 列表的数据处理（列表的方法）\n",
    "\n",
    "> 1. 查询数据\n",
    ">> 1. index() 按照索引值查询数据，并取出值\n",
    ">> 2. slice切片方法取值\n",
    ">> 3. in方法查询元素是否存在\n",
    "> 2. 增加数据\n",
    ">> 1. append()增加在末尾\n",
    ">> 2. insert() 指定位置增加\n",
    "> 3. 删除数据\n",
    ">> 1. pop()指定位置删除\n",
    ">> 2. remove() 指定结果（值）删除\n",
    ">> 3. clear() 清空列表\n",
    "> 4. 修改数据\n",
    ">> 1. 找到index进行修改\n",
    "> 5. 其他方法\n",
    ">> 1. 排序： sort() reverse()\n",
    ">> 2. 复制：copy()\n",
    "\n",
    "## 2. 元组\n",
    "\n",
    "## 3. 列表实践\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dcad885",
   "metadata": {},
   "source": [
    "## 1.1 列表的删除\n",
    "\n",
    ">1. pop()\n",
    ">> 1. 默认删除列表中最后一位数据\n",
    ">> 2. 指定位置进行删除\n",
    ">> 3. 注意事项：避免index error的出现，先检查列表长度：len()\n",
    ">2. remove()\n",
    ">> 1. 指定结果（值）删除\n",
    ">> 2. 注意事项：避免value error 的出现，先检查元素是否存在in\n",
    ">3. clear()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09d5920c",
   "metadata": {},
   "source": [
    "### 1. pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c6ad55d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李小龙']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李小龙\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "17ce8990",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c2b5494f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李小龙'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "efb7b603",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李小龙'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "20aae4cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "781eba83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c0ad8df9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '小红']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e48bc7d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先检查列表长度\n",
    "len(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7681fd16",
   "metadata": {},
   "source": [
    "### 2. remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1a6f0b52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "adaa9872",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李小龙']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李小龙\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a4e05b14",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[17], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m name_list\u001b[38;5;241m.\u001b[39mremove(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m小红\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "name_list.remove(\"张三\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a4c9f5a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove(\"张三\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b0351707",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '李小龙']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d23c442a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先查询是否存在该值\n",
    "\"小红\" in name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2c86a1b1",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[23], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m name_list\u001b[38;5;241m.\u001b[39mremove(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m小红\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "name_list.remove(\"小红\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12b3a68e",
   "metadata": {},
   "source": [
    "### 3. clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "14800bdc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李小龙']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李小龙\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "050c3786",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5dbe73e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d56892ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d2075eb",
   "metadata": {},
   "source": [
    "## 2. 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b94196d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 17, 19, 20, 21]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list = [18,19,18,17,19,20,21]\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "30298679",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "af29d891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 18, 18, 19, 19, 20, 21]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f172817b",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "936ebe60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[21, 20, 19, 19, 18, 18, 17]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e97d0ef4",
   "metadata": {},
   "source": [
    "## 3. 复制 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "2fb2bfdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李小龙']\n",
      "2240643921984\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李小龙\"]\n",
    "print(name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "3bccf855",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李小龙']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "存档_name_list = name_list\n",
    "存档_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b1e6da31",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"melyon\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f905c4a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李小龙', 'melyon']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "915baee6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李小龙', 'melyon']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 请问：存档_name_list 的结果会不会发生变化？\n",
    "存档_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9c9410e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李小龙']\n"
     ]
    }
   ],
   "source": [
    "# 综上，并不能真正的复制，接下来我们看一下copy\n",
    "name_list = [\"小明\",\"张三\",\"小红\",\"李小龙\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "060a5024",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李小龙']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "copy_name_list = name_list.copy()\n",
    "copy_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "1cf8a5a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"melyon\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "c1999ce3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李小龙', 'melyon']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e4c0977a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李小龙']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "copy_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eda7daab",
   "metadata": {},
   "source": [
    "## 2. 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "3e7b3566",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = 12345, 54321, 'hello!'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "5f1a3744",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello!')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(t)\n",
    "print(type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "fbfade8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321, 'hello!')"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (12345, 54321, 'hello!')\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "e3f65d08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "240b847f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "3cef21d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.index(54321)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "528fec86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.count(12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e2b43b32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2023, tm_mon=11, tm_mday=8, tm_hour=11, tm_min=31, tm_sec=26, tm_wday=2, tm_yday=312, tm_isdst=0)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例：\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "7f90ab37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1699414383.6121233"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d102d38",
   "metadata": {},
   "source": [
    "# 实践：\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cb818e4",
   "metadata": {},
   "source": [
    "购物车程序项目：\n",
    "```\n",
    "   要求:1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "        2、让用户输入商品编号进行商品的购买。\n",
    "        3、用户选择商品后，查询用户的余额是否足够，若够则直接付款，不够则提醒用户。\n",
    "        4、用户随时可以退出购买，退出时打印自己已购买的商品和支付宝余额。\n",
    "\n",
    "```\n",
    "1. alipay = input（）\n",
    "\n",
    "商品列表 = [\n",
    "\n",
    "]\n",
    "\n",
    "购物车 = []\n",
    "\n",
    "print(商品列表)\n",
    "\n",
    "2.user_choose = input()\n",
    "\n",
    "if 商品价格 >  alipay\n",
    "\n",
    "      1.\n",
    "else:\n",
    "\n",
    "      2.\n",
    "      \n",
    "3. 购物车.append（购买到的商品）\n",
    "\n",
    "\n",
    "4."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c9904321",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "please input your saving:1\n",
      "选择购买商品编号：[退出:q]0\n",
      "编码不存在\n",
      "选择购买商品编号：[退出:q]1\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'product_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 11\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m choice\u001b[38;5;241m.\u001b[39misdigit():\n\u001b[0;32m     10\u001b[0m     choice \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(choice)\n\u001b[1;32m---> 11\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m choice \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m choice \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlen\u001b[39m(product_list):  \n\u001b[0;32m     12\u001b[0m         p_item \u001b[38;5;241m=\u001b[39m product_list[choice\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m     14\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m p_item[\u001b[38;5;241m1\u001b[39m] \u001b[38;5;241m<\u001b[39m saving:\n",
      "\u001b[1;31mNameError\u001b[0m: name 'product_list' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "saving = input(\"please input your saving:\")\n",
    "if saving.isdigit():\n",
    "    saving = int(saving)\n",
    "    while True:\n",
    "\n",
    "            #引导用户选择商品\n",
    "            choice = input('选择购买商品编号：[退出:q]')\n",
    "            #验证输入是否合法\n",
    "            if choice.isdigit():\n",
    "                choice = int(choice)\n",
    "                if choice > 0 and choice <= len(product_list):  \n",
    "                    p_item = product_list[choice-1]\n",
    "    \n",
    "                    if p_item[1] < saving:\n",
    "                        saving-=p_item[1]\n",
    "                        shopping_car.append(p_item)\n",
    "\n",
    "\n",
    "                    else:\n",
    "                        print(\"余额不足，还剩下%s\"%saving)\n",
    "                    print(p_item)\n",
    "                else:\n",
    "                    print('编码不存在')\n",
    "\n",
    "\n",
    "            elif choice == \"q\":\n",
    "                print(\"--------您已经购买以下商品----------\")\n",
    "               \n",
    "                for i in shopping_car:\n",
    "                    print(i)\n",
    "                print(\"您还剩*元钱\"%saving)\n",
    "                break;\n",
    "            else:\n",
    "                print(\"invalid input\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7606876c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的余额：10000\n",
      "---------校园二手市场---------\n",
      "商品编号: 0, 商品名称: 电瓶车, 商品价格: 600 元\n",
      "商品编号: 1, 商品名称: Iphone-15, 商品价格: 8999 元\n",
      "商品编号: 2, 商品名称: Mate-60, 商品价格: 7999 元\n",
      "商品编号: 3, 商品名称: 珍珠奶茶, 商品价格: 7 元\n",
      "商品编号: 4, 商品名称: Python书, 商品价格: 89 元\n",
      "请输入商品编号:0\n",
      "电瓶车 已购买成功,您的余额还有: 9400 元\n",
      "---------校园二手市场---------\n",
      "商品编号: 0, 商品名称: 电瓶车, 商品价格: 600 元\n",
      "商品编号: 1, 商品名称: Iphone-15, 商品价格: 8999 元\n",
      "商品编号: 2, 商品名称: Mate-60, 商品价格: 7999 元\n",
      "商品编号: 3, 商品名称: 珍珠奶茶, 商品价格: 7 元\n",
      "商品编号: 4, 商品名称: Python书, 商品价格: 89 元\n",
      "请输入商品编号:2\n",
      "Mate-60 已购买成功,您的余额还有: 1401 元\n",
      "---------校园二手市场---------\n",
      "商品编号: 0, 商品名称: 电瓶车, 商品价格: 600 元\n",
      "商品编号: 1, 商品名称: Iphone-15, 商品价格: 8999 元\n",
      "商品编号: 2, 商品名称: Mate-60, 商品价格: 7999 元\n",
      "商品编号: 3, 商品名称: 珍珠奶茶, 商品价格: 7 元\n",
      "商品编号: 4, 商品名称: Python书, 商品价格: 89 元\n",
      "请输入商品编号:3\n",
      "珍珠奶茶 已购买成功,您的余额还有: 1394 元\n",
      "---------校园二手市场---------\n",
      "商品编号: 0, 商品名称: 电瓶车, 商品价格: 600 元\n",
      "商品编号: 1, 商品名称: Iphone-15, 商品价格: 8999 元\n",
      "商品编号: 2, 商品名称: Mate-60, 商品价格: 7999 元\n",
      "商品编号: 3, 商品名称: 珍珠奶茶, 商品价格: 7 元\n",
      "商品编号: 4, 商品名称: Python书, 商品价格: 89 元\n",
      "请输入商品编号:1\n",
      "就1394元，还想买Iphone-15,逛一逛别的商品吧~\n",
      "---------校园二手市场---------\n",
      "商品编号: 0, 商品名称: 电瓶车, 商品价格: 600 元\n",
      "商品编号: 1, 商品名称: Iphone-15, 商品价格: 8999 元\n",
      "商品编号: 2, 商品名称: Mate-60, 商品价格: 7999 元\n",
      "商品编号: 3, 商品名称: 珍珠奶茶, 商品价格: 7 元\n",
      "商品编号: 4, 商品名称: Python书, 商品价格: 89 元\n",
      "请输入商品编号:0\n",
      "电瓶车 已购买成功,您的余额还有: 794 元\n",
      "---------校园二手市场---------\n",
      "商品编号: 0, 商品名称: 电瓶车, 商品价格: 600 元\n",
      "商品编号: 1, 商品名称: Iphone-15, 商品价格: 8999 元\n",
      "商品编号: 2, 商品名称: Mate-60, 商品价格: 7999 元\n",
      "商品编号: 3, 商品名称: 珍珠奶茶, 商品价格: 7 元\n",
      "商品编号: 4, 商品名称: Python书, 商品价格: 89 元\n",
      "请输入商品编号:quit\n",
      "您的购物车: [['电瓶车', 600], ['Mate-60', 7999], ['珍珠奶茶', 7], ['电瓶车', 600]], 您的余额还剩: 794 元\n"
     ]
    }
   ],
   "source": [
    "# 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "alipay = input(\"请输入你的余额：\")\n",
    "\n",
    "商品列表 = [\n",
    "    [\"电瓶车\",600],\n",
    "    [\"Iphone-15\",8999],\n",
    "    [\"Mate-60\",7999],\n",
    "    [\"珍珠奶茶\",7],\n",
    "    [\"Python书\",89]\n",
    "   \n",
    "]\n",
    "\n",
    "购物车 = [] # 当用户选择商品时，增加进购物车\n",
    "\n",
    "while True:  \n",
    "    \n",
    "    \n",
    "    print('---------校园二手市场---------')\n",
    "    for i in 商品列表:\n",
    "        print(\"商品编号: %s, 商品名称: %s, 商品价格: %s 元\"%(商品列表.index(i),i[0],i[1]))\n",
    "\n",
    "    # 2. 让用户输入商品编号进行商品的购买\n",
    "    # 3. 用户选择商品后，检查用户的余额是否足够，若够则直接付款，不够则提醒用户。\n",
    "\n",
    "    user_choose = input(\"请输入商品编号:\")\n",
    "    # 条件判断1： 判断用户输入的商品编号还是其他？\n",
    "\n",
    "    if user_choose.isdigit():\n",
    "    # 条件判断2：商品价格 和 余额，两种可能，使用if条件判断\n",
    "        if 商品列表[int(user_choose)][1] < int(alipay):\n",
    "            # 1. 余额 > 商品价格，购买成功\n",
    "            alipay = int(alipay) - 商品列表[int(user_choose)][1]\n",
    "            购物车.append(商品列表[int(user_choose)])\n",
    "            print(\"%s 已购买成功,您的余额还有: %s 元\"%(商品列表[int(user_choose)][0],alipay))\n",
    "        else:\n",
    "            print(\"就%s元，还想买%s,逛一逛别的商品吧~\"%(alipay,商品列表[int(user_choose)][0]))\n",
    "\n",
    "\n",
    "    # 4. 用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额。\n",
    "    elif user_choose ==\"quit\":\n",
    "        print(\"您的购物车: %s, 您的余额还剩: %s 元\"%(购物车,alipay))\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入正确的商品编号，或者输入 'quit' 退出\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb1d0201",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
